package book.BouncySquare;

import  java.nio.ByteBuffer;
import  java.nio.ByteOrder;
import  java.nio.FloatBuffer;
import  java.nio.IntBuffer;

import  javax.microedition.khronos.opengles.GL10;           //1 
import  javax.microedition.khronos.opengles.GL11;

import android.content.Context;
import android.graphics.*;
import android.opengl.*;

import android.util.Log;

/**
 * A vertex shaded square. 
 */
class Square
{
    private String TAG="rtsp";

    public FloatBuffer mTextureBuffer;
    float[] textureCoords =
            {
                    0.0f, 1f,
                    1f, 1f,

                    0.0f, 0.0f,
                    1f, 0.0f,

            };

    public int createTexture(GL10 gl, Context contextRegf, int resource)
    {
        Bitmap image = BitmapFactory.decodeResource(contextRegf.getResources(),
                resource);                                                           // 1

        IntBuffer maxSize = IntBuffer.allocate(10);
        gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize);        //2

        Bitmap.Config conf = image.getConfig();             //3

        if(null != conf)
            Log.v(TAG, String.format("%d x %d, maxsize:%d  conf:%s", image.getWidth(), image.getHeight(), maxSize.get(0), conf.toString()));
        else
            Log.v(TAG, "no confdd");


        gl.glGenTextures(1, textures, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);


        int mx = maxSize.get(0);
        int pixSize = 4;

        int h = 320;
        int w = 480;

        ByteBuffer vbb = ByteBuffer.allocateDirect(mx * mx * pixSize);
        vbb.order(ByteOrder.nativeOrder());

        ///just for test
//        for(int i = 0; i<h; i++)
//        {
//            if( i < 32 )
//            {
//                for(int j= 0; j<w; j++)
//                {
//                    Byte b0 = 0;
//                    Byte b1 = (byte)255;
//
//                    vbb.put(i * w * 4 + j*4 + 0, b0);
//                    vbb.put(i * w * 4 + j*4 + 1, b1);
//                    vbb.put(i * w * 4 + j*4 + 2, b0);
//                    vbb.put(i * w * 4 + j*4 + 3, b0);
//                }
//            }
//            else
//            {
//                for(int j= 0; j<w; j++)
//                {
//                    Byte b0 = 0;
//                    Byte b1 = (byte)255;
//
//                    vbb.put(i * w * 4 + j*4 + 0, b0);
//                    vbb.put(i * w * 4 + j*4 + 1, b0);
//                    vbb.put(i * w * 4 + j*4 + 2, b0);
//                    vbb.put(i * w * 4 + j*4 + 3, b0);
//                }
//            }
//        }
//        vbb.position(0);
        ///end test

        image.copyPixelsToBuffer(vbb);
        w = image.getWidth();
        h = image.getHeight();

        vbb.position(0);            ///4

//        gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, w, h, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, vbb);
        gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, mx, mx, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, null);
        gl.glTexSubImage2D(GL10.GL_TEXTURE_2D, 0, 0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, vbb);        ///5

//        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_LINEAR);


        image.recycle();      ///6
        return  resource;
    }

    public Square()
    {
        float vertices[] =           //2


                {
                        -1.0f, -1.0f,
                        1.0f, -1.0f,
                        -1.0f,  1.0f,
                        1.0f,  1.0f
                };

        byte maxColor=(byte)255;

        byte colors[] =                //3
                {
                        maxColor,maxColor,       0,maxColor,
                        0,       maxColor,maxColor,maxColor,
                        0,              0,       0,maxColor,
                        maxColor,       0,maxColor,maxColor
                };

        byte indices[] =               //4
                {
                        0, 3, 1,
                        0, 2, 3
                };

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);      //5
        vbb.order(ByteOrder.nativeOrder());
        mFVertexBuffer = vbb.asFloatBuffer();
        mFVertexBuffer.put(vertices);
        mFVertexBuffer.position(0);

        mColorBuffer = ByteBuffer.allocateDirect(colors.length);
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
        mIndexBuffer.put(indices);
        mIndexBuffer.position(0);

        ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
        tbb.order(ByteOrder.nativeOrder());
        mTextureBuffer = tbb.asFloatBuffer();
        mTextureBuffer.put(textureCoords);
        mTextureBuffer.position(0);

    }

    public void draw(GL10 gl)                                           //6
    {
        //7
        gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);            //8
        //gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);          //9

        gl.glEnable(GL10.GL_TEXTURE_2D);      //1
        // gl.glEnable(GL10.GL_BLEND);       //2
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_SRC_COLOR);    //3
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);   //4

        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);    //5

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);                        //9
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);   //6

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);    //7

        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);   //8

    }

    private FloatBuffer mFVertexBuffer;
    private ByteBuffer   mColorBuffer;
    private ByteBuffer  mIndexBuffer;
    private int[] textures = new int[1];
}


